/*
 * Copyright 2011-2012 The ProjectSample Organization
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.projectsample.simplest.s1.security;

import java.sql.SQLException;

import org.projectsample.simplest.s1.exception.DbException;
import org.projectsample.simplest.dbutils.Session;
import org.projectsample.simplest.dbutils.SessionFactory;

/**
 * @author Jason Xing
 */
public class Destroy {
    
    public void dropDb() {
        String sql = "";
        Session session = null;
        try {
            session = SessionFactory.openSession();
            // 用户的角色
            sql = "drop table user_role";
            session.execute(sql);
            // 用户
            sql = "drop table \"user\"";
            session.execute(sql);
            // 角色的权限
            sql = "drop table role_permission";
            session.execute(sql);
            // 角色
            sql = "drop table role";
            session.execute(sql);
            // 权限
            sql = "drop table permission";
            session.execute(sql);
            // 菜单（前台）。resource的一种，继承自resource。
            sql = "drop table menu";
            session.execute(sql);
            // 资源
            sql = "drop table resource";
            session.execute(sql);
            session.commit();
        } catch (SQLException e) {
            session.rollback();
            throw new DbException(e);
        } finally {
            session.close();
        }
    }

    public void deleteData() {
        String sql = "";
        Session session = null;
        try {
            session = SessionFactory.openSession();
            sql = "delete from user_role";
            session.execute(sql);
            sql = "delete from \"user\"";
            session.execute(sql);
            sql = "delete from role_permission";
            session.execute(sql);
            sql = "delete from role";
            session.execute(sql);
            sql = "delete from permission";
            session.execute(sql);
            sql = "delete from menu";
            session.execute(sql);
            sql = "delete from resource";
            session.execute(sql);
            session.commit();
        } catch (SQLException e) {
            session.rollback();
            throw new DbException(e);
        } finally {
            session.close();
        }
    }

}
